<template>
  <div>
    <Box>
      <h2 style="margin: 10px 15px" slot="title">上报基本信息</h2>
      <div slot="box">
        <div>
          <a-steps :current="current">
            <a-step v-for="item in steps" :key="item.title" :title="item.title" />
          </a-steps>
          <div class="steps-content">
            <div v-show="this.current == 0">
              <basic ref="basic" :basics="current" @basicses="basicses" @fileListse="fileListse" @passCheck="passCheck">
              </basic>
            </div>
            <div v-show="this.current == 1">
              <suspect ref="suspect" :suspects="current" @basicses1="basicses1" @passCheck="passCheck"></suspect>
            </div>
            <div v-show="this.current == 2">
              <victim ref="victim" :victims="current" @basicses2="basicses2" @passCheck="passCheck"></victim>
            </div>
          </div>
          <div class="steps-action">
            <a-button v-if="current < steps.length - 1" type="primary" @click="next">
              下一步
            </a-button>
            <a-button v-if="current == steps.length - 1" type="primary" @click="success">
              完成
            </a-button>
            <a-button v-if="current > 0" style="margin-left: 8px" @click="prev">
              上一步
            </a-button>
          </div>
        </div>
      </div>
    </Box>
  </div>
</template>

<script>
import Box from '@views/xlgc/Box'
import { getDictItemsFromCache } from '@api/api'
import basic from '@/views/xlgc/lawCase/contradiction/detailed/basic'
import suspect from '@/views/xlgc/lawCase/contradiction/detailed/suspect'
import victim from '@/views/xlgc/lawCase/contradiction/detailed/victim'
import { getdataDet, getMAshangbao, updataForm, updateDisputes } from '@/api/xlgc/lawCase'
import { getAgencyDetails } from '@api/xlgc/ComprehensiveBusiness'
export default {
  name: 'upload',
  components: { Box, basic, suspect, victim },
  data() {
    return {
      pass: false,
      fileLists: [],
      fileList: [],
      labelCol: { span: 10 },
      wrapperCol: { span: 6 },
      other: '',
      control: {},
      suspect: {},
      victim: {},
      form: {},
      dateFormat: 'YYYY-MM-DD',
      current: 0,
      steps: [
        {
          title: '事件基本信息',
          content: '2',
        },
        {
          title: '主要当事人',
          content: '2',
        },
        {
          title: '化解情况',
          content: '2',
        },
      ],
      identification: false
    };
  },
  mounted() {
    // alert("11111")
    if (this.$route.query.id) {
      this.identification = true
      // alert(6)
      this.institutionD(this.$route.query.id)
    }
  },
  methods: {
    institutionD(val) {
      getdataDet(val).then(response => {
        console.log("response")
        console.log(response)
        //基本信息
        this.control = response.result.disputes
        // this.nodeVOS = response.result.nodeVOS
        //矛盾当事人
        this.party = response.result.party
        //化解信息
        this.resolution = response.result.resolution

        this.$refs.basic.setControl(this.control, response.result.fileList)
        this.$refs.suspect.setControl(this.party)
        this.$refs.victim.setControl(this.resolution)

      })
        .catch(error => {
          console.log(error)
        })
    },
    passCheck(val) {
      this.pass = val
    },
    fileListse(val) {
      this.fileLists = val
    },
    success() {
      console.log(this.fileLists, this.control, this.suspect, this.victim)
      let arr = []
      arr.push(this.victim)
      let data = {
        'disputes': this.control,
        // 'fileList':this.fileLists,
        'party': this.suspect,
        'resolution': arr
      }
      this.$set(data, 'fileList', this.fileLists)
      this.$refs.victim.submitForm()
      // this.fileLists == [] ? Object.assign(data,{'fileList':this.fileLists}):this.fileLists =[]
      console.log(data)
      if (this.pass) {
        if (!this.identification) {
          // alert(6)
          updataForm(data).then(response => {
            console.log(response)
            if (response.success) {
              this.$message.success('上报成功', 2,);
              this.$router.push({
                path: '/mine/list'
              })
            } else {
              this.$message.error('添加失败，请仔细核对后再次添加');
            }

          }).catch(error => {
            console.log(error)
          })
        } else {

          updateDisputes(data).then(response => {
            if (response.success) {
              this.$message.success('修改成功', 2)
              this.$router.push({
                path: '/mine/list'
              })
            } else {
              this.$message.error('修改失败，请仔细核对后再次添加')
            }
          }).catch(error => {
            console.log(error)
          })
        }

      } else {
        this.$message.error('您此页的数据还没有填写完整,无法提交');
      }
    },
    basicses(val) {
      this.control = Object.assign({}, val)
      console.log(val)
    },
    basicses1(val) {
      val = JSON.stringify(val)
      val = JSON.parse(val)
      console.log(val)
      this.suspect = Object.assign({}, val)
    },
    basicses2(val) {
      val = JSON.stringify(val)
      val = JSON.parse(val)
      console.log(val)
      this.victim = Object.assign({}, val)
      // this.victim.push(val)
    },
    next() {
      if (this.current == 0) {
        this.$refs.basic.submitForm()
        // alert(this.pass)
        if (!this.pass) {
          this.$message.error('您此页的数据还没有填写完整,无法进行下一项');
        }
        else {
          this.current++;
        }
      }
      else if (this.current == 1) {
        this.$refs.suspect.submitForm()
        if (!this.pass) {
          this.$message.error('您此页的数据还没有填写完整,无法进行下一项');
        } else {
          this.current++;
        }
      }
      // if(this.current == 0){
      //   this.$refs.basic.submitForm()
      // }
      // if(this.current == 1){
      //   this.$refs.suspect.submitForm()
      // }
      // if(this.current == 2){
      //   this.$refs.victim.submitForm()
      // }
      //
      // console.log(this.pass)
      // if(this.pass){
      //   this.current++;
      //   this.pass = false
      // }else{
      //   this.$message.error('您此页的数据还没有填写完整,无法进行下一项');
      // }
    },
    prev() {
      this.current--;
    },
    onSubmit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          // alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.ruleForm.resetFields();
    },
    onChange(date, dateString) {
      console.log(date, dateString);
    },
    onChange1(date, dateString) {
      console.log(date, dateString);
    },

    handleChange1(val) {
      this.formData.level = val
      return val
    },
    handleChange2(val) {
      this.formData.type = val
      return val
    },
    handleRemove(file) {
      const index = this.fileList.indexOf(file);
      const newFileList = this.fileList.slice();
      newFileList.splice(index, 1);
      this.fileList = newFileList;
    },
    beforeUpload(file) {
      this.fileList = [...this.fileList, file];
      return false;
    },

    filterMultiDictText(dictCode, text) {
      console.log(dictCode, text)
      if (!text && text !== 0) return
      let datas = getDictItemsFromCache(dictCode)
      // return this.$filterMultiDictText(datas, text)
      return this.$filterDictText(datas, text)
    },
  },
  watch: {
    form: {
      handler(a, b) {
        console.log(this.form)
      }
    }
  }

}
</script>

<style scoped>
.steps-content {
  margin-top: 20px;
  /*border: 1px dashed #e9e9e9;*/
  border-radius: 6px;
  /*background-color: #fafafa;*/
  min-height: 200px;
  /*text-align: center;*/
  padding-top: 5px;
}

.steps-action {
  margin-top: 24px;
}

:deep(.ant-steps) {
  margin: 0 auto !important;
  width: 60% !important;
}
</style>